#ifndef NCFTPD_MONITOR_H
#define NCFTPD_MONITOR_H

#include "../ftpmonitorloop.h"

class NcftpdMonitor : public FTPMonitorLoop
{
	public:
		NcftpdMonitor(QString whoPath,bool useSudo)
		:FTPMonitorLoop()
		{
			createExecCommand(useSudo,whoPath);
		};

		//<tr><td>anonymous@127.0.0.1</td>  <td>(none)</td> <td>linux-oms7.lan</td> 
		//<td>11:38:44</td><td>0:00:00</td><td align=center>2</td> <td>32414</td> 
		//<td><TT><font size=-2>/srv/ftp</font></TT></td> <td><TT><font size=-2>List /srv/ftp</font></TT></td>   </tr>
	
		//<tr><td>anonymous@127.0.0.1</td>  <td>(none)</td> <td>linux-oms7.lan</td> 
		//<td>11:38:44</td><td>0:00:00</td><td align=center>2</td> <td>32414</td> 
		//<td><TT><font size=-2>/srv/ftp</font></TT></td> <td><TT><font size=-2>Retr /srv/ftp/bigfile.test</font></TT></td> </tr>
		void processStdOut(QString stdout)
		{
			QRegExp rx;
			QVector<QStringList> listOfClients;
			QStringList clientRow;
		
			QString rawOut=stdout.simplified();
			int first=rawOut.indexOf("<tr>");
			int last=rawOut.lastIndexOf("</tr>");
			rawOut=rawOut.mid(first);
			rawOut=rawOut.left(last);
		
			QStringList processList = rawOut.split("</tr>");
			QStringList::Iterator it = processList.begin();
			if(it == processList.end())
				return;
			rx.setMinimal(true);
			for (;it != processList.end(); ++it){
				rx.setPattern(".*<td>([^<]*)</td>.*<td>([^<]*)</td>.*<td>([^<]*)</td>.*<td>([^<]*)</td>.*<td>([^<]*)</td>.*<td>([^<]*)</td>.*>([^<]*)</font>.*>([^<]*)</font>.*$");
				if (rx.indexIn(*it) != -1){
					//kDebug(1234) << "1:" << rx.cap(1)<< "2:"<< rx.cap(2)<< "3:" << rx.cap(3) 
					//<< "4:"<< rx.cap(4) << "5:"<< rx.cap(5)<< "6:"<< rx.cap(6) << "7:"<< rx.cap(7) << "8:"<< rx.cap(8);
					clientRow.clear();
					clientRow << rx.cap(6) << rx.cap(1) << rx.cap(8) << rx.cap(7) << rx.cap(2);
					listOfClients.push_back(clientRow);
				}
			}
			searchForClientsStateChange(listOfClients);
		}

		/*
		 *
		 */
		void createExecCommand(bool useSudo,QString whoPath)
		{
			if (whoPath=="")
				mFtpWhoCommand.bin = "/usr/local/sbin/" + ftpWhoBinaryName();
			else
				mFtpWhoCommand.bin = whoPath + ftpWhoBinaryName();

			mFtpWhoCommand.args << "-h" << "who";

			//if we use sudo, then ftpwho binary is first argumen of sudo command
			if (useSudo==true)
			{
				mFtpWhoCommand.args.prepend(mFtpWhoCommand.bin);
				//The -n (non-interactive) option prevents sudo from prompting the user
				//for a password.  If a password is required for the command to run, sudo
				//will display an error messages and exit.
				mFtpWhoCommand.args.prepend("-n");
				mFtpWhoCommand.bin="sudo";
			}
	
		}
		
		/*
		 *
		 */
		bool ftpClientDiff(QStringList newClientRow,QStringList oldClientRow)
		{
			//on 1 position is login however this looks to be a ncftpd bug sometimes it show on this place password sometimes host ip
			if ( newClientRow.at(1) != oldClientRow.at(1) || newClientRow.at(2) != oldClientRow.at(2) ||
			     newClientRow.at(3) != oldClientRow.at(3))
				return true;
			else
				 return false;
		}

		/*
		 *
		 */
		QStringList headerLabels()
		{
			return QStringList() << "PID" << "Login" << "State" << "Location" << "Host";
		}

		/*
		 *
		 */
		FtpServerType ftpServerType()
		{
			return FTPMonitorLoop::ncftpd;
		}

		/*
		 *
		 */
		QString ftpdServerName()
		{
			return QString("ncftpd");
		}

		QString static ftpWhoBinaryName()
		{
			return QString("ncftpd_spy");
		}
};

#endif
